package com.michael.demos.base.java.serializable;

import java.io.*;

/**
 * 类功能描述:
 * <pre>
 *   xxxx
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/8/18 10:22
 */
public class SerializableTest {


	private static final String path = "D:\\111.txt";

	private static void serialize(User user) throws Exception {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(path)));
		oos.writeObject(user);
		oos.close();
	}

	private static User deserialize() throws Exception {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(path)));
		return (User) ois.readObject();
	}


	public static void main(String[] args) throws Exception {
		User user = new User();
		user.setName("Michael");
		user.setAge(18);
		user.setGender("男");
		System.out.println("序列化前的结果: " + user);

		serialize(user);

		User dUser = deserialize();
		System.out.println("反序列化后的结果: " + dUser);
	}
}
